
package net.pec.geotools_svg.svg_conversion;


import java.io.*;
import java.net.URL;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.geotools.data.*;
import org.geotools.feature.*;
import org.opengis.feature.simple.SimpleFeature;

import com.vividsolutions.jts.geom.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class PolylineElementGenerator {

private static double middleY;
private boolean evenOdd = true;
private StringBuffer pointValues=new StringBuffer();

  
public void transformFeature(SimpleFeature feature, Document doc, GenerateMapLayer gElement, 
		Double midpoint, 
		int strokeRed, int strokeGreen, int strokeBlue,  double strokeopacity, Double strokewidth, String clickEvent  ) throws Exception {
  middleY = midpoint;
  //Double middleY=(feature.getBounds().getMaxY()+feature.getBounds().getMinY())/2;
	Coordinate[] coord;
  Coordinate oneCrd = new Coordinate();
  String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  Element polylineElement = doc.createElementNS(svgNS, "polyline");
  polylineElement.setAttributeNS(null, "id", feature.getID());
  polylineElement.setAttributeNS(null, "style","fill:none;stroke:rgb("+strokeRed+","+strokeBlue+","+strokeGreen+")" +
  		";stroke-width:"+strokewidth+";stroke-opacity:"+strokeopacity);
  polylineElement.setAttributeNS(null, "marker-mid", "url(#MidMarker)");
  if (clickEvent !=null){
	  polylineElement.setAttributeNS(null, "onclick", clickEvent);
	  }
  
  Geometry geom = (Geometry) feature.getDefaultGeometry();
  
    
  
	  if (geom instanceof LineString ){
	   
	   coord = ((LineString)geom).getCoordinates();
	   
		   for ( int i = 0 ; i < coord.length ; i++ ){
		    
		    double coorddiff = coord[i].y - middleY;
			   double newcoord = middleY-coorddiff;
			   oneCrd = coord[i];
			   pointValues.append(oneCrd.x + "," + newcoord + " ");
		    
		   }
		   polylineElement.setAttributeNS(null, "points", pointValues.toString());
			gElement.addFeatureElements(polylineElement);  	   
  } 
	  
	 
	  if (geom instanceof MultiLineString ){		   
		  for ( int j = 0 ; j < ((GeometryCollection)geom).getNumGeometries() ; j++){
			  
			    Geometry childGeom = ((GeometryCollection)geom).getGeometryN(j);
			    getGeometryCollection(feature, childGeom, doc,gElement,middleY,
			    		strokeRed,strokeBlue,strokeGreen,strokeopacity,strokewidth, clickEvent);
			   }
		  
		  middleY = midpoint;
		  //Double middleY=(feature.getBounds().getMaxY()+feature.getBounds().getMinY())/2;
			
		   svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
		   polylineElement = doc.createElementNS(svgNS, "polyline");
		  polylineElement.setAttributeNS(null, "id", feature.getID());
		  polylineElement.setAttributeNS(null, "style","fill:none;stroke:rgb("+strokeRed+","+strokeGreen+","+strokeBlue+")" +
		  		";stroke-width:"+strokewidth+";stroke-opacity:"+strokeopacity);
		  
		  //polylineElement.setAttribute("marker-mid", "url(#MidMarker)");
		  
		  
		  if (clickEvent !=null){
			  polylineElement.setAttributeNS(null, "onclick", clickEvent);
			  }
		  polylineElement.setAttributeNS(null, "points", pointValues.toString());			
		  gElement.addFeatureElements(polylineElement);  
	  }
	  
	  
	  
	  } 

	  private void transformGeometryCollection(SimpleFeature feature, Geometry geom, Document doc, GenerateMapLayer gElement, Double midpoint, 
				int strokeRed, int strokeGreen, int strokeBlue,  double strokeopacity, Double strokewidth, String clickEvent ) throws Exception {
		  middleY = midpoint;
		  //Double middleY=(feature.getBounds().getMaxY()+feature.getBounds().getMinY())/2;
			Coordinate[] coord;
		  Coordinate oneCrd = new Coordinate();
		  String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
		  Element polylineElement = doc.createElementNS(svgNS, "polyline");
		  polylineElement.setAttributeNS(null, "id", feature.getID());
		  polylineElement.setAttributeNS(null, "style","fill:none;stroke:rgb("+strokeRed+","+strokeBlue+","+strokeGreen+")" +
		  		";stroke-width:"+strokewidth+";stroke-opacity:"+strokeopacity);
		  if(evenOdd==false){
			  evenOdd=true;
		  polylineElement.setAttribute("marker-start", "url(#MidMarker)");}
		  else{evenOdd=false;}
		  
		  if (clickEvent !=null){
			  polylineElement.setAttributeNS(null, "onclick", clickEvent);
			  }
		  
		  
		  String pointValues = "";
		    
		  
			  if (geom instanceof LineString ){

				    
			      coord = ((LineString)geom).getCoordinates();
			   
				   for ( int i = 0 ; i < coord.length ; i++ ){				    
				    double coorddiff = coord[i].y - middleY;
					   double newcoord = middleY-coorddiff;
					   oneCrd = coord[i];
					   pointValues = pointValues+oneCrd.x + "," + newcoord + " ";
				    
				   }
				  
				    polylineElement.setAttributeNS(null, "points", pointValues);
					gElement.addFeatureElements(polylineElement);  	   
			  								} 
			  
			  
			}
	  
	  
	  private void getGeometryCollection(SimpleFeature feature, Geometry geom, Document doc, GenerateMapLayer gElement, Double midpoint, 
				int strokeRed, int strokeGreen, int strokeBlue,  double strokeopacity, Double strokewidth, String clickEvent ) throws Exception {
		 
		  
		  middleY = midpoint;
		  //Double middleY=(feature.getBounds().getMaxY()+feature.getBounds().getMinY())/2;
			Coordinate[] coord;
		  Coordinate oneCrd = new Coordinate();
		  
		  
		    
		  
			  if (geom instanceof LineString ){

				    
			      coord = ((LineString)geom).getCoordinates();
			   
				   for ( int i = 0 ; i < coord.length ; i++ ){				    
				    double coorddiff = coord[i].y - middleY;
					   double newcoord = middleY-coorddiff;
					   oneCrd = coord[i];
					   pointValues.append(oneCrd.x + "," + newcoord + " ");
				    
				   }
				  
				    //polylineElement.setAttributeNS(null, "points", pointValues);
					//gElement.appendChild(polylineElement);  	   
			  								} 
			  
			  
			}


	  

}